<%@ page language="java" import="java.util.*"  pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%
String path=request.getContextPath();
String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.or/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<base href="<%=basePath %>">
<title>规则配置</title>
<%@ include file="/WEB-INF/jsp/common/common.jsp" %>
<script src="<%=path%>/js/jquery.divbox.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
	
})
function Map(){
	  this.elements = new Array();
	   //获取MAP元素个数
	   this.size = function() {
	       return this.elements.length;
	   };
	   //判断MAP是否为空
	   this.isEmpty = function() {
	       return (this.elements.length < 1);
	   };
	   //删除MAP所有元素
	   this.clear = function() {
	       this.elements = new Array();
	   };
	   //向MAP中增加元素（key, value)
	   this.put = function(_key, _value) {
	       if(this.containsKey(_key)){
	        this.remove(_key);
	        this.elements.push( {
	           key : _key,
	           value : _value
	        });
	       }else{
	        this.elements.push( {
	           key : _key,
	           value : _value
	        });
	       }  
	   };
	   //删除指定KEY的元素，成功返回True，失败返回False
	   this.remove = function(_key) {
	       var bln = false;
	       try {
	           for (var i = 0; i < this.elements.length; i++) {
	               if (this.elements[i].key == _key) {
	                   this.elements.splice(i, 1);
	                   return true;
	               }
	           }
	       } catch (e) {
	           bln = false;
	       }
	       return bln;
	   };
	   //获取指定KEY的元素值VALUE，失败返回NULL
	   this.get = function(_key) {
	       try {
	           for (var i = 0; i < this.elements.length; i++) {
	               if (this.elements[i].key == _key) {
	                   return this.elements[i].value;
	               }
	           }
	       } catch (e) {
	           return false;
	       }
	       return false;
	   };
	   //判断MAP中是否含有指定KEY的元素
	   this.containsKey = function(_key) {
	       var bln = false;
	       try {
	           for (var i = 0; i < this.elements.length; i++) {
	               if (this.elements[i].key == _key) {
	                   bln = true;
	               }
	           }
	       } catch (e) {
	           bln = false;
	       }
	       return bln;
	   };
	  //获取map的所有values
	  this.values = function() {
	       var arr = new Array();
	       for (var i = 0; i < this.elements.length; i++) {
	           arr.push(this.elements[i].value);
	       }
	       return arr;
	   };
	  //获取map的所有keys
	  this.keys= function() {
	       var arr = new Array();
	       for (var i = 0; i < this.elements.length; i++) {
	           arr.push(this.elements[i].key);
	       }
	       return arr;
	   };
	   //获取数组
	   this.getArray= function(){
	   	var arr = new Array();
	   	for (var i = 0; i < this.elements.length; i++) {
	           arr.push({
		            key : this.elements[i].key,
		            value : this.elements[i].value
		         });
	       }
	       return arr;
	   };
	}
var mapCheck = new Map();
var map = new Map();
function vreset(){
	$("#ruleId").val("");
	$("#effDate").val("");
	$("#termDate").val("");
	$("#ruleAgentDesc").val("");
	map.clear();
	$("#agentType3Table").find("tbody").remove();
	$('#sale2').hide();
}
function addRuleAgent() {
	var strReg = "[0-9]{4}-[0-9]{2}-[0-9]{2}";
	var ruleId = $("#ruleId").val();
	var ruleAgentId = "";
	var effDate =$("#effDate").val();
	var termDate =$("#termDate").val();
	var premium =$("#premium").val();
	var ruleAgentDesc =$("#ruleAgentDesc").val();
	var status =$("#status").val();
	$("td[id='ruleAgentId']").each(function(){
		if(""==ruleAgentId){
			ruleAgentId+= $(this).html();
		}else{
			ruleAgentId+= ","+$(this).html();
		}
	});
	 if(ruleId.length==0){
	    	$.message("error","请选择规则");
	    	return false;
	    }
	 if(ruleAgentId.length==0){
	    	$.message("error","请选择代理人");
	    	return false;
	    }
	 if(null != effDate && "" != effDate){
			if(effDate.match(strReg)==null){
				$.message("error", "您输入的定义起期格式不正确，格式为:YYYY-MM-DD");
				return;
			}
		}
		if(null != termDate && "" != termDate){
			if(termDate.match(strReg)==null){
				$.message("error", "您输入的定义止期格式不正确，格式为:YYYY-MM-DD");
				return;
			}
		}
	/* $("input[name='radioAgentInfo']:checked").each(function(){
		objCont+="<tr>"
	        +"<td id='ruleAgentId' style='width:10%;display:none'>"+$(this).parent().parent().children(" td:eq(1)").html()+"</th>"
	        +"<td style='width:20%' align='center'>"+$(this).parent().parent().children(" td:eq(2)").html()+"</th>"
	        +"<td style='width:20%' align='center'>"+$(this).parent().parent().children(" td:eq(3)").html()+"</th>"
	        +"<td style='width:20%' align='center'>"+$(this).parent().parent().children(" td:eq(4)").html()+"</th>"
	        +"<td style='width:20%' align='center'>"+$(this).parent().parent().children(" td:eq(5)").html()+"</th>"
	        +"<td style='width:20%' align='center'>"+$(this).parent().parent().children(" td:eq(6)").html()+"</th>"
    	+"</tr>"
	}) */
	$.openLoading();	
	$.ajax({
		url:"<%=path%>/ruleAgentConfig/addRuleAgent?"+new Date().getTime(),
		type:"POST",
		async : true,
		dataType:'json',
		data:{
			ruleId:ruleId,
			ruleAgentIds:ruleAgentId,
			effDate:effDate,
			termDate:termDate,
			premium:premium,
			ruleAgentDesc:ruleAgentDesc,
			status:status
			},
		success : function(obj) {
			//表单状态
			Formstatus=true;
			$.closeLoading();
			if($.validate(obj))return;
			$.dialog({
				type: "success",//success,error
				content: "添加成功,点击确定跳转列表",
				onCancel:function(){
					
				},
				onOk: function() {
					openRuleAgentList();
					return;
				}
			});
			
		}
	});
		
}
function openRuleAgentList(){
	window.location.href='<%=basePath%>ruleAgentConfig/list';
}

//点击“选择(规则)”
function openRuleDiv(){
	queryRuleInfo(1);
}

//为查询规则拼接参数
function queryRuleInfo(pageIndex){
	var ruleName = $.trim($('#ruleName_choose').val());
	var planCode = $.trim($('#planNo_choose').val());
	loadRuleInfo({current:pageIndex,size:8,ruleName:ruleName,planCode:planCode});
}

//加载规则
function loadRuleInfo(param){
	$.openLoading();
	$.ajax({
		url : "<%=path%>/ruleAgentConfig/queryRulePage?"+new Date().getTime(),
		type : 'POST',
		async : true,
		data:param,
		dataType:'json',
		success:function(page){
			$.closeLoading();
			if($.validate(page))return;
			var firstbutton ="<input class='input_fxk' name='radioRuleInfo' type='radio' value=''>";
			var tableContent = creatTable1("id,ruleName,planCode,partnerNo",page,firstbutton);
			var tableObj = $("#ruleInfoTable");
			tableObj.find("tbody").remove();
			tableObj.append(tableContent);
			var strPage = creatChannelPage(param.current,6,page.maxPage,"93");
			var pageDIV = $("#ruleInfoPageDIV");
			pageDIV[0].innerHTML="";
			pageDIV.append(strPage);
			$('#ruleInfoDiv').OpenDiv();
			bindradio();
		}
	});
}  

//点击“确定”按钮，选择规则
function chooseRuleInfo(){
	var selected = $("input[name='radioRuleInfo']:checked");
	if(selected.length==0){
		$.message("error", "请选择一个规则");
		return;
	}
	var ruleId = selected.parent().parent().children(" td:eq(1)").html();
	var planNo = selected.parent().parent().children(" td:eq(3)").html();
	$('#ruleId').val(ruleId);
	$('#planNo').val(planNo);
	closeRuleInfoDiv();
}

//关闭 选择规则 弹出层
function closeRuleInfoDiv(){
	$("#ruleInfoDiv").CloseDiv();	
	$('#ruleName_choose').val("");
	$('#planNo_choose').val("");
}
//点击“配置代理信息”
function openAgentDiv(){
	queryAgentInfoRela3(1);
}

//为查询代理信息 拼接参数
function queryAgentInfoRela3(pageIndex){
	var planNo = $.trim($('#planNo').val());
	var ruleId = $.trim($('#ruleId').val());
	if(ruleId==null||""==ruleId){
		$.message("error", "请先选择一个规则");
		return;
	}
	loadAgentInfo({current:pageIndex,size:8,planNo:planNo,ruleId:ruleId});
}

//加载代理信息
function loadAgentInfo(param){
	$.openLoading();
	$.ajax({
		url : "<%=path%>/ruleAgentConfig/queryAgentInfoByPlanNo?"+new Date().getTime(),
		type : 'POST',
		async : true,
		data:param,
		dataType:'json',
		success:function(page){
			$.closeLoading();
			if($.validate(page))return;
			var firstbutton ="<input class='input_fxk' name='radioAgentInfo' type='checkbox' value=''>";
			var tableContent = creatTable1("ID@TDH,USER_CODE,C_BRKR_NME,DPT_CDE,EMP_NO,EMP_NAME",page,firstbutton);
			var tableObj = $("#agentInfoTable");
			tableObj.find("tbody").remove();
			tableObj.append(tableContent);
			var strPage = creatChannelPage(param.current,6,page.maxPage,"45");
			$("#agentInfoCheckAll").prop("checked",false);
			var pageDIV = $("#agentInfoPageDIV");
			pageDIV[0].innerHTML="";
			pageDIV.append(strPage);
			$("#agentInfoTable").find("tr").each(function(){
				var id = $(this).children("td:eq(1)").html();
				if(map.containsKey(id)){
					$(this).addClass("table_highlighter");
					$(this).children("td:eq(0)").find("input[name='radioAgentInfo']").prop("checked",true);
				}
			});
			$('#agentInfoDiv').OpenDiv();
			bindredio();
		}
	});
} 

function bindredio(){
	$("input[type='radio']").click(function(){
		$("input[name='radioRuleInfo']").parent().parent().removeClass("table_highlighter");
		$(this).parent().parent().addClass("table_highlighter");

   	});
	$("#agentInfoCheckAll").click(function(){
		if($(this).prop("checked")){//选中
			$("#agentInfoTable tbody tr").each(function() {
				$(this).find("input[name='radioAgentInfo']").prop("checked",true);
				$(this).addClass("table_highlighter");
				var obj = new Object();
				obj.id = $(this).children("td:eq(1)").html();
				obj.userCode = $(this).children("td:eq(2)").html();
				obj.userName = $(this).children("td:eq(3)").html();
				obj.dept = $(this).children("td:eq(4)").html();
				obj.empNo = $(this).children("td:eq(5)").html();
				obj.empName = $(this).children("td:eq(6)").html();
				map.put(obj.id,obj);
			});
		}else{//取消选中
			$("#agentInfoTable tbody tr").each(function() {
				$(this).removeClass("table_highlighter");
				$(this).find("input[name='radioAgentInfo']").prop("checked",false);
				var id= $(this).children("td:eq(1)").html();
				map.remove(id);
			});
		}
	});
	$("input[name='radioAgentInfo']").click(function(){
		var id= $(this).parent().parent().children("td:eq(1)").html();
		if($(this).prop("checked")){//选中
			$(this).parent().parent().addClass("table_highlighter");
			var obj = new Object();
			obj.id = id;
			obj.userCode = $(this).parent().parent().children("td:eq(2)").html();
			obj.userName = $(this).parent().parent().children("td:eq(3)").html();
			obj.dept = $(this).parent().parent().children("td:eq(4)").html();
			obj.empNo = $(this).parent().parent().children("td:eq(5)").html();
			obj.empName = $(this).parent().parent().children("td:eq(6)").html();
			map.put(obj.id,obj);
		}else{//取消选中
			$(this).parent().parent().removeClass("table_highlighter");
			$(this).prop("checked",false);
			map.remove(id);
		}
   	});
}

//点击“确定”按钮，选择规则
function chooseAgentInfo(){
	var date="";
	var tableObj=$("#agentType3Table");
	var objCont="";
	var arr = map.values();
	for(var i=0;i<arr.length;i++){
		var obj=arr[i];
		objCont+="<tr>"
	        +"<td id='ruleAgentId' style='width:10%;display:none'>"+obj.id/* $(this).parent().parent().children(" td:eq(1)").html() */+"</th>"
	        +"<td style='width:20%' align='center'>"+obj.userCode/* $(this).parent().parent().children(" td:eq(2)").html() */+"</th>"
	        +"<td style='width:20%' align='center'>"+obj.userName/* $(this).parent().parent().children(" td:eq(3)").html() */+"</th>"
	        +"<td style='width:20%' align='center'>"+obj.dept/* $(this).parent().parent().children(" td:eq(4)").html() */+"</th>"
	        +"<td style='width:20%' align='center'>"+obj.empNo/* $(this).parent().parent().children(" td:eq(5)").html() */+"</th>"
	        +"<td style='width:20%' align='center'>"+obj.empName/* $(this).parent().parent().children(" td:eq(6)").html() */+"</th>"
    	+"</tr>"
	}
	if(objCont==""){
		$.message("error", "请选择代理人");
		return;
	}
	tableObj.find("tbody").remove();
	tableObj.append(objCont);
	$('#sale2').show();
	closeAgentInfoDiv();
}

//关闭 选择 弹出层
function closeAgentInfoDiv(){
	$("#agentInfoDiv").CloseDiv();	
}

</script>
</head>
<body>
	<div class="current">
		<table class="current_table_1">
			<tbody>
				<tr class="f2">
				<td><img src="./images/current_1.jpg"></td>
				 <td class="current_table_1_td">
                   <img src="images/current_1.png">&nbsp;您现在的位置：&nbsp;
                     <span style=" color:#136bab; ">界面出单管理
                     <span style="padding:0 5px; font-weight:bold; line-height:28px;">&gt;</span>录单二维码管理
                     <span style="padding:0 5px; font-weight:bold; line-height:28px;">&gt;</span>录单二维码新增
                 </span>
                 </td>
                 <td><img src="./images/current_3.jpg"></td>
				</tr>
			</tbody>
		</table>
	</div>
	<form id="inputForm">
	        <table class="table_05">
              <tbody>
	              <tr>
	                  <td colspan="4" style=" font-size:12px; font-weight:bold; text-align:left;color:#3876bf; line-height:17px; background-image:url(images/centent_1.jpg)">
							录单二维码新增
	                  </td>
	              </tr>
	              <tr>
	                  <th>规则编号：</th>
	                  <td style="width: 300px;">
	                  	<input name="ruleId" id="ruleId" readonly="readonly" style="background-color: #ccc;width: 150px" class="input_text"><span class="red">*</span>&nbsp;
	                  	<input type="button"  class="input_button_2" id="searchRuleBtn" value="请选择" onClick="openRuleDiv(1,this)"/>
	                  </td>
	                  <th>状态：</th>
		                <td>
		                  <select id="status" class="select_1" style="visibility: visible;width: 150px">
		                  	<option selected="selected" value="1">有效</option>
		                  	<option value="0">无效</option>
		                  </select>
		                </td>
	              </tr>
	              <tr>
	             	  <th>有效起期：</th>
	                  <td>
						<input id="effDate" style="width: 150px" onkeydown="false" onclick="showCalendar('effDate','y-mm-dd');" readonly="readonly" class="input_time" type="text" />
	                  </td>
	             	  <th>有效止期：</th>
	                  <td>
						<input id="termDate" style="width: 150px" onkeydown="false" onclick="showCalendar('termDate','y-mm-dd');" readonly="readonly" class="input_time" type="text" />
	                  </td>
	               </tr>
				  <tr>
					  <th>保费：</th>
					  <td>
						  <input id="premium" style="width: 150px"   type="text" />
					  </td>
				  </tr>
	               <tr>
	               		<th>二维码描述：</th>
	                  <td colspan="3" >
	                  	<textarea name="ruleAgentDesc" id="ruleAgentDesc" rows="5" class="textarea_1" style="resize:none;"></textarea>
	                  </td>
	              </tr>
	              <tr id="sale1" >
		                  <th>代理信息配置：</th>
		                  <td colspan="3"><input type="button" onclick="openAgentDiv()" value="代理信息配置"/>
		              </tr>
		              <tr id="sale2" style="display:none">
		             	 <th></th>
		                  <td colspan="4">
		                  	<div style="width:100%; overflow:scroll;">
			                  	<table id="agentType3Table"  style="width:99%;" class="content_table_1">
			                  		<thead>
								<tr class="f1">
				                  <th style="width:10%; display: none">序号</th>
				                  <th style="width:20%" >代理人编码</th>
				                  <th style="width:20%" >代理人名称</th>
				                  <th style="width:20%" >出单机构</th>
				                  <th style="width:20%" >业务员代码</th>
				                  <th style="width:20%" >业务员名称</th>
				              	</tr>
									</thead>
									<tbody id="agentShowDiv">
						            </tbody>
			                  	</table>
		                  	</div>
		                  </td>
		              </tr>	
	               <tr style="display: none">
	               		<th>方案代码：</th>
	                  <td>
	                  	<input name="planNo" id="planNo" >&nbsp;
	                  </td>
	              </tr>
		</tbody>
		</table>
		</form>
		<div align="center" style="margin-top: 7px;">
		<input id="sub" class="input_button_1" name="" type="button"
			value="保存" onclick="addRuleAgent()">&nbsp;&nbsp; <input
			class="input_button_1" name="" onclick="vreset()" type="button"
			value="重置">
		</div>
	<!-- 选择 合作伙伴 div-->
  	<div id="ruleInfoDiv" style="display:none;width:90%;text-align: center;">
		<div class="table_title_div">
				<table class="table_title" style='width:100%;height: 20px;'>
					<tr>
					<td width="10%"></td>
					<td width="80%" id="interfaceName"></td>
					<td width="10%" align="right"><img src="./images/closedown.gif" alt="关闭"  onclick="closeRuleInfoDiv()"/></td>
					</tr>
				</table>
		</div>
		<table class="table_02" style="width:98%">
			<tr>
				<td>
					<table class="search_table">
						<tr height="30px">
							<td colspan="3" style="text-align:center">
								规则名称：<input name="txt3"  id="ruleName_choose" class="input_text" />&nbsp;&nbsp;
								方案代码：<input name="txt3" id="planNo_choose" class="input_text" />&nbsp;&nbsp;
								<input type="button"  class="input_button_1" value="查询" onClick="queryRuleInfo(1)"/>
							</td>
						</tr>
					</table>
				</td>
			</tr>
			<tr>
				<td>
					<table id="ruleInfoTable"  class="content_table_1">
						<thead>
							<tr class="f1">
			                  <th style="width:3%">选择</th>
			                  <th style="width:5%">规则ID</th>
			                  <th style="width:6%">规则名称</th>
			                  <th style="width:10%">方案代码</th>
			                  <th style="width:10%">合作伙伴</th>
			              	</tr>
						</thead>
						<tbody>
						</tbody>
					</table>
					<div id="ruleInfoPageDIV"  class="page"></div>
				</td>
			</tr>
		</table>
		<div align="center">
		   <input type="button" value="确定" onclick="chooseRuleInfo()" class="input_button_3"/>&nbsp;&nbsp;&nbsp;
		   <input type="button" value="取消" onclick="closeRuleInfoDiv()" class="input_button_3"/>
		</div>
	</div>	
	<!-- 选择 规则 div-->
  	<div id="agentInfoDiv" style="display:none;width:90%;text-align: center;">
		<div class="table_title_div">
				<table class="table_title" style='width:100%;height: 20px;'>
					<tr>
					<td width="10%"></td>
					<td width="80%" id="interfaceName"></td>
					<td width="10%" align="right"><img src="./images/closedown.gif" alt="关闭"  onclick="closeAgentInfoDiv()"/></td>
					</tr>
				</table>
		</div>
		<table class="table_02" style="width:98%">
			<tr>
				<td>
					<table id="agentInfoTable"  class="content_table_1">
						<thead>
							<tr class="f1">
			                  <th style="width:3%"><input id="agentInfoCheckAll" type="checkbox"></th>
			                  <th style="width:3%;display: none">序号</th>
			                  <th style="width:5%">代理人编码</th>
			                  <th style="width:5%">代理人名称</th>
			                  <th style="width:6%">出单机构</th>
			                  <th style="width:6%">业务员代码</th>
			                  <th style="width:7%">业务员名称</th>
			              	</tr>
						</thead>
						<tbody>
						</tbody>
					</table>
					<div id="agentInfoPageDIV"  class="page"></div>
				</td>
			</tr>
		</table>
		<div align="center">
		   <input type="button" value="确定" onclick="chooseAgentInfo()" class="input_button_3"/>&nbsp;&nbsp;&nbsp;
		   <input type="button" value="取消" onclick="closeAgentInfoDiv()" class="input_button_3"/>
		</div>
	</div>	
</body>
</html>